package com.ybwh.springboot2.leafid.controller;

import com.sankuai.inf.leaf.service.SegmentService;
import com.sankuai.inf.leaf.service.SnowflakeService;
import com.ybwh.springboot2.common.RespData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Api(tags = "ID生成器")
@Controller
@RequestMapping("/")
public class LeafidController {


    @Autowired
    private SegmentService segmentService;
//    @Autowired
    private SnowflakeService snowflakeService;


    @ApiOperation(value = "号段算法id", notes = "")
    @ResponseBody
    @GetMapping("/segmentid")
    public RespData<Long> segmentid() {
        // leaf-segment-test 是业务标识
        return RespData.successResp(segmentService.getId("leaf-segment-test").getId());
    }


    @ApiOperation(value = "雪花算法id", notes = "")
    @ResponseBody
    @GetMapping("/snowflakeid")
    public RespData<Long> snowflakeid() {
        return RespData.successResp(snowflakeService.getId("leaf-segment-test").getId());
    }


    @ApiOperation(value = "上传文件", notes = "")
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RespData upload(@ApiParam(value = "上传文件", required = true) @RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {


        file.transferTo(new File("D:/tmp/" + file.getOriginalFilename()));


        return RespData.successResp(null);

    }


}
